package com.example.resturant.service.impl;

import com.example.resturant.model.Menu;
import com.example.resturant.repository.MenuRepository;
import com.example.resturant.service.MenuService;
import com.example.resturant.vo.MenuVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuRepository menuRepository;

    @Override
    public Page<MenuVO> listMenus(Pageable page) {
        Page<Menu> menus = menuRepository.findAll((Specification<Menu>) (root, cq, cb) -> cb.equal(root.get("deleted"), false), page);
        return menus.map(menu -> {
            MenuVO menuVO = new MenuVO();
            menuVO.setId(menu.getId());
            menuVO.setName(menu.getName());
            menuVO.setPrice(menu.getPrice());
            menuVO.setImage(menu.getImage());
            return menuVO;
        });
    }
}
